export const isPasswordExpired = (forceModifyCredentialTime) => {
    if (new Date(forceModifyCredentialTime).valueOf() - new Date().valueOf() < 0) {
        return "Expired"
    }
    if (new Date(forceModifyCredentialTime).valueOf() - new Date().valueOf() < 864000000) {
        return "ExpiredIn30Days"
    }
    return "Valid"
}

export const isPasswordStrong = (rule, value, callback) => {
    // 如果没有内容，继续执行
    if (!value) return callback()
    // 如果有内容，则判断
    const reg =
        /^.*(?=.{8,20})(?=.*\d)(?=.*[A-Z]{1,})(?=.*[a-z]{1,})(?=.*[!@#$%^&*?,.;'<>:"\-_=+\/\|\\\{\}\[\]\(\)]).*$/
    const isPasswordStrong = reg.test(value)
    // 逻辑
    if (!isPasswordStrong)
        return callback(new Error('密码最短8位，必须包含数字，小写字母，大写字母和特殊字符'))
    callback()
}

export const debounce = (func, delay) => {
    let timer = null;
    return function (...args) {
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(() => {
            func.apply(this, args);
        }, delay);
    };
}

export const arrayToElTreeSelectData = (items) => {
    const result = [] // 存放结果集
    const itemMap = {} //
    for (const item of items) {
        const id = item.id
        const parentId = item.parentId
        if (!itemMap[id]) {
            itemMap[id] = {
                children: [],
            }
        }
        itemMap[id] = {
            ...item,
            children: itemMap[id]['children'],
        }
        const treeItem = itemMap[id]
        treeItem.label = treeItem.name
        treeItem.value = treeItem.id
        if (!parentId) {
            result.push(treeItem)
        } else {
            if (!itemMap[parentId]) {
                itemMap[parentId] = {
                    children: [],
                }
            }
            itemMap[parentId].children.push(treeItem)
        }
    }
    return result
}
